// pages/result/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex:0,
    xAxis:[],//
    yAxis:[],
    start_time:"",
    end_time:"",
    show:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.changeTab()
   
  },
  // 切换时间段
  changeTab(e){
    let index = e ? Number(e.target.dataset.index) : 0 ;
    Date.prototype.format = function (fmt) {
      var o = {
          "M+": this.getMonth() + 1, //月份
          "d+": this.getDate(), //日
          "h+": this.getHours(), //小时
          "m+": this.getMinutes(), //分
          "s+": this.getSeconds(), //秒
          "q+": Math.floor((this.getMonth() + 3) / 3), //季度
          "S": this.getMilliseconds() //毫秒
      };
      if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      for (var k in o)
          if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      return fmt;
  }
    let start_time,end_time;
    let dateObj = new Date();
    let time = dateObj.getTime();
    end_time = dateObj.format('yyyy-MM-dd')
    if(index < 3){
      if(index == 0) start_time = new Date(time - 1000 * 3600*24 * 30).format('yyyy-MM-dd');
      if(index == 1) start_time =   new Date(time - 1000 * 3600*24 * 90).format('yyyy-MM-dd');
      if(index == 2) start_time =  new Date(time - 1000 * 3600*24 * 180).format('yyyy-MM-dd');
      this.setData({
        tabIndex:index,
        start_time:start_time,
        end_time:end_time
      })
     this.getData()
    }else{
      this.setData({
        tabIndex:index
      })
    }
  },
  bindDateChange(e){
    let value = e.detail.value
    if(e.target.id == 1){
      this.setData({
        end_time:value
      })
    }else{
      this.setData({
        start_time:value
      })
    }
    this.getData()
  },
  getData(){
    app.post('exercise/exerciseEffect',{
      startDay:this.data.start_time,
      endDay:this.data.end_time,
    }).then(r=>{
        let res = r.data;
        let arr = [
          res.right_vision,
          res.left_vision,
          res.both_vision
        ]
        this.setData({
          yAxis:arr,
          show:true
        })
    })
  }
})